do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

foreach x in first {
foreach y in 5 {
	capture log close
	log using "$log\4.FigA2_PlaceboTest_DDD_`x'_`y'_`c(current_date)'.log" , replace
	
	dis in red "Sample: `x'; Cutoff: `y'k"
	dis "Date: `c(current_time)', Time: `c(current_time)'"
		
	qui{
		clear
		set obs 9
		gen parm = _n
		gen coef = .
		gen se = .
		expand 1000
		bysort parm: gen id = _n

		sort id parm

		save "$wdata\placebo_`x'_`y'.dta", replace
		
		use "$wdata\project_lottery_fertility_b3a6_`x'_`y'k.dta", clear
		drop if prize10M > 5
		
		keep if norm == 0
		keep houseid current prize5M
		sort current houseid
		save "$wdata\prize_data_`x'_`y'.dta", replace
	
	}
	
	forv a = 1(1)1000{ //Placebo test for 1,000 times
		dis "-- Placebo `a'..."
		qui{
		set seed `a'
		
		use "$wdata\prize_data_`x'_`y'.dta", clear
		gen random = rnormal()
		sort current random
		drop houseid random
		merge 1:1 _n using "$wdata\prize_data_`x'_`y'.dta", nogen
		
		merge 1:m houseid using "$wdata\project_lottery_fertility_b3a6_`x'_`y'k.dta"
		drop if _m == 2
		drop _m

		** Interaction Terms
		drop currentxprize
		gen currentxprize = 0
		replace currentxprize = prize5M if current == 1
		
		gen totbaby_diff = totbaby - totbaby_pre1 
		
		forv i = 1(1)3{
			gen v1 = pers_real_prize_aftertax if norm == -`i'
			egen pre`i'prize = mean(v1), by(houseid)
			drop v1
			recode pre`i'prize . = 0
		}
		
		** Interaction Terms
		replace currentxprize = prize5M if current == 1
		forv j = 3(-1)2{
			replace currentxprizexpre`j' = 0
			replace currentxprizexpre`j' = prize5M if current == 1 & norm_year==-`j'
		}
		forv j = 0(1)6{
			replace currentxprizexpost`j' = 0
			replace currentxprizexpost`j' = prize5M if current == 1 & norm_year==`j'
		}	
		
		replace norm = norm + 3
		
		reghdfe totbaby currentxprizexp* prize5M c.prize5M#i.norm c.prize5M#1.current i.age $control1 $control2 $control3, a(norm#current year) cl(houseid)

		matrix b = e(b)
		matrix V = e(V)
		
		forv i = 1(1)9{
			local coef`i' = b[1,`i']
			local se`i' =  sqrt(V[`i',`i'])
		}
		
		use "$wdata\placebo_`x'_`y'.dta", clear
		
		forv i = 1(1)9{
			replace coef = `coef`i'' if parm == `i' & id == `a'
			replace se = `se`i'' if parm == `i' & id == `a'
		}
		
		save "$wdata\placebo_`x'_`y'.dta", replace
		}
	}
	dis "Completed! Date: `c(current_time)', Time: `c(current_time)'"
	log close
}
}

* Main Estimate
foreach x in first {
foreach y in 5  {
	use "$wdata\project_lottery_fertility_b3a6_`x'_`y'k.dta", clear
	drop if prize10M > 5
	
	forv i = 1(1)3{
		gen v1 = pers_real_prize_aftertax if norm == -`i'
		egen pre`i'prize = mean(v1), by(houseid)
		drop v1
		recode pre`i'prize . = 0
	}
	
	foreach z in income asset{
		gen tot_`z' = pers_`z' + spouse_`z'
		gen v1 = tot_`z' if norm == -1
		egen pre1_`z' = mean(v1), by(houseid)
		drop v1
	}
	
	replace norm_year = norm_year + 3
	
	reghdfe totbaby currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3, a(norm#current year) cl(houseid)

	matrix b = e(b)
	matrix V = e(V)
	
	forv i = 1(1)9{
		local coef`i' = b[1,`i']
		local se`i' =  sqrt(V[`i',`i'])
	}
	
	clear
	set obs 9
	gen id = 0
	gen parm = _n
	gen coef = .
	gen se = .
	
	forv i = 1(1)9{
		replace coef = `coef`i'' if parm == `i'
		replace se = `se`i'' if parm == `i'
	}
		
	save "$wdata\main_estimate_`x'_`y'.dta", replace
}
}

use "$wdata\main_estimate_first_5.dta",clear
ap using "$wdata\placebo_first_5.dta"

sum coef if parm == 9 & id == 0
loc main = r(mean)
loc pos = `main' - 0.0005

cap gen x = `main'
cap gen y = 0 in 1
cap replace y = 0.1 in 2

twoway 	(hist coef if parm == 9 & id != 0, fc(gs12) lc(gs8) frac) ///
		(line y x, lc(maroon)), ///
		xline(`main', lc(maroon)) ///
		xscale(ra(-0.063 0.063)) xlabel(-0.06(0.02)0.06,format(%4.2f)) ///
		text(0.09 `pos' "Main Estimate", place(w) color(maroon)) ///
		ylabel(0(0.02)0.10,angle(0) format(%4.2f)) scheme(s1color) legend(off) ///
		xtitle(Estimates) ytitle(Fraction)
graph export "$figure/Figure_E1a.png", as(png) replace	

gen norm = parm - 4
replace norm = norm + 1 if norm >= -1

set obs 10010
replace norm = -1 if norm == .
replace id = _n - 9010 if id == .
replace coef = 0 if norm == -1

sort id norm 
local figure = ""

forv i = 1(1)1000{
	loc figure = `"`figure' (line coef norm if id == `i', lc(gs12%50) lw(vthin))"'
}

twoway 	`figure' ///
		(scatteri 0 -3 0 6, recast(line) lc(black)) ///
		(connect coef norm if id == 0, lc(maroon) mc(maroon) ms(O)), ///
		leg(col(3) order(1002 "Main Estimate" 1 "Placebo Estimates") size(small)) ///
		scheme(s1color) xlabel(-3(1)6) xtitle(Years from Lottery Prize) ///
		ytitle(Estimates) ylabel(-0.06(0.02)0.06, angle(0) format(%4.2f))
graph export "$figure/Figure_E1b.png", as(png) replace	
